package com.siwei.mes.service.erp.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.siwei.mes.common.query.PageQuery;
import com.siwei.mes.common.result.ApiResult;
import com.siwei.mes.common.result.Json;
import com.siwei.mes.common.util.JsonUtils;
import com.siwei.mes.entity.dto.PrintExperimentInfo;
import com.siwei.mes.entity.erp.RwdextraPrintInfo;
import com.siwei.mes.entity.erp.RwdextraPrintRecord;
import com.siwei.mes.entity.experiment.concrete.batch.ConcreteKyqd;
import com.siwei.mes.mapper.erp.RwdextraPrintRecordMapper;
import com.siwei.mes.service.erp.RwdextraExtendService;
import com.siwei.mes.service.erp.RwdextraPrintRecordService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * ERP任务单打印记录(RwdextraPrintRecord)表服务实现类
 *
 * @author linzi
 * @since 2024-08-31 23:39:07
 */
@Service
public class RwdextraPrintRecordServiceImpl implements RwdextraPrintRecordService {
    @Resource
    private RwdextraPrintRecordMapper rwdextraPrintRecordMapper;
    @Resource
    private RwdextraExtendService rwdextraExtendService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(RwdextraPrintRecord entity) {
        List<Integer> frwdhList = entity.getFrwdhList();
        JSONArray printExperimentInfos = entity.getPrintJsonList();
        int con = 0;
        for (Integer frwd : frwdhList) {
            entity.setId(null);
            entity.init(true);
            entity.setFrwdh(frwd);
            JSONObject printJson = JSON.parseObject(JSON.toJSONStringWithDateFormat(printExperimentInfos.get(con),"yyyy-MM-dd HH:mm:ss",
                    SerializerFeature.DisableCircularReferenceDetect));
            JSONArray printJsonArray = new JSONArray();
            printJsonArray.add(printJson);
            entity.setPrintJson(JsonUtils.beanToObject(Json.succ().data(ApiResult.addList(printJsonArray))));
            rwdextraPrintRecordMapper.insertSelective(entity);

            rwdextraExtendService.isPrint(entity.getFrwdh());
            con++;
        }
    }

    @Override
    public void update(RwdextraPrintRecord entity) {
        RwdextraPrintRecord rwdextraPrintRecord = rwdextraPrintRecordMapper.selectByPrimaryKey(entity.getId());
        assertTrue(rwdextraPrintRecord == null, "ERP任务单打印记录信息不存在");
        entity.init(true);
        rwdextraPrintRecordMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public void delete(Long id) {
        RwdextraPrintRecord rwdextraPrintRecord = rwdextraPrintRecordMapper.selectByPrimaryKey(id);
        assertTrue(rwdextraPrintRecord == null, "ERP任务单打印记录信息不存在");
        rwdextraPrintRecordMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<RwdextraPrintRecord> getPage(PageQuery<RwdextraPrintRecord, RwdextraPrintRecord> query) {
        RwdextraPrintRecord searchParam = query.getParams();
        return PageHelper.startPage(query.getPageNum(), query.getPageSize())
                .doSelectPageInfo(() -> rwdextraPrintRecordMapper.getList(searchParam));
    }

    @Override
    public RwdextraPrintRecord loadById(Long id) {
        return rwdextraPrintRecordMapper.loadById(id);
    }

    @Override
    public List<RwdextraPrintRecord> getAll(RwdextraPrintRecord entity) {
        return rwdextraPrintRecordMapper.getList(entity);
    }
}
